package com.example.test.conview.view.core;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.puty.conputyutils.LogUtils;
import com.puty.conputyview.R;
import com.puty.conputyview.common.PutyConst;
import com.puty.conputyview.view.attributes.Barcode2dAttriutes;
import com.puty.conputyview.view.attributes.BaseAttributes;
import com.puty.conputyview.view.tool.DetaultLabelInfo;
import com.puty.conputyview.view.tool.DimensionCodeUtils;

/**
 * 二维码处理，没有用上的
 */
@Deprecated
public class QrCodeElement extends Element {
    private final static String TAG = "QrCodeElement";

    Bitmap img2d = null;
    /**
     * 原始图
     */
    Bitmap sourceImage = null;

    public QrCodeElement(Context context, String content, Float x, Float y, float width,
                         float height, Label lb) {
        super(context, lb);
        this.mContext = context;
        super.type = PutyConst.QR_CODE;
        this.content = content;
        super.left = x;
        super.top = y;
        this.width = width * lb.scalingRatio;
        this.height = height * lb.scalingRatio;
        BaseAttributes attributes = DetaultLabelInfo.load(mContext, DetaultLabelInfo.ATTRS_BARCODE2D);
        if (attributes != null) {
            Barcode2dAttriutes barcode2dAttributes = (Barcode2dAttriutes) attributes;
            encoding = barcode2dAttributes.encode;
            blankSpace = barcode2dAttributes.blankSpace;
            errorCorrectionLevel = barcode2dAttributes.errorCorrectionLevel;
        }
    }

    @Override
    public void init() {
        BarcodeFormat barcodetype;
        switch (barcodeType) {
            case 1:
                barcodetype = BarcodeFormat.DATA_MATRIX;
                break;
            case 2:
                barcodetype = BarcodeFormat.PDF_417;
                break;
            default:
                barcodetype = BarcodeFormat.QR_CODE;
                break;
        }
        String encodingString = "utf-8";
        switch (encoding) {
            case 2:
                encodingString = "gbk";
                break;
            case 3:
                encodingString = "iso-8859-1";
                break;
        }
        ErrorCorrectionLevel el;
        switch (errorCorrectionLevel) {
            case 0:
                el = ErrorCorrectionLevel.L;
                break;
            case 1:
                el = ErrorCorrectionLevel.M;
                break;
            case 2:
                el = ErrorCorrectionLevel.H;
                break;
            default:
                el = ErrorCorrectionLevel.Q;
                break;
        }
        LogUtils.i(TAG,"二维码");
        if(TextUtils.isEmpty(this.content)){
            this.content = mContext.getString(R.string.text);
        }
        LogUtils.i("qr","el:"+el);
        this.sourceImage = DimensionCodeUtils.qrcreateCodeImage(this.content, (int) super
                .width, (int) super.height, barcodetype, encodingString, blankSpace, el,
                isShowLogo,PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES ? 2 : 0);
        if (this.sourceImage == null)
            return;
        rate(this.rate);
    }

    @Override
    public void rate(int degree) {
        Matrix matrix = new Matrix();
        matrix.setRotate(degree, this.sourceImage.getWidth() / 2, this.sourceImage.getHeight() / 2);
        this.img2d = Bitmap.createBitmap(this.sourceImage, 0, 0, this.sourceImage.getWidth(),
                this.sourceImage.getHeight(), matrix, true);
        this.rate = degree;
    }

    @Override
    public void zoom(float x, float y, float width, float height) {
        super.zoom(x, y, width, height);
        if (zoomdirect == 0) {
            this.width += x;
            this.height += x;
            if (this.width <= PutyConst.RATE_BOUNDARY) {
                this.width = PutyConst.RATE_BOUNDARY;
            } else  if(this.left + this.width + PutyConst.RATE_INCREASE >= width) {
                this.width -= x;
                this.height -= x;
                return;
            }

            if (this.height <= PutyConst.RATE_BOUNDARY) {
                this.height = PutyConst.RATE_BOUNDARY;
            } else if(this.top + this.height + PutyConst.RATE_INCREASE >= height) {
                this.width -= x;
                this.height -= x;
                return;
            }
        } else {
            this.width += y;
            this.height += y;
            if (this.width <= PutyConst.RATE_BOUNDARY) {
                this.width = PutyConst.RATE_BOUNDARY;
            } else  if(this.left + this.width + PutyConst.RATE_INCREASE >= width) {
                this.width -= y;
                this.height -= y;
                return;
            }

            if (this.height <= PutyConst.RATE_BOUNDARY) {
                this.height = PutyConst.RATE_BOUNDARY;
            } else if(this.top + this.height + PutyConst.RATE_INCREASE >= height) {
                this.width -= y;
                this.height -= y;
                return;
            }
        }
        zoomOtherSelected(x, y);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (this.img2d == null) {
            return;
        }
        Rect sr = new Rect(0, 0, this.img2d.getWidth(), this.img2d.getHeight());

        canvas.drawBitmap(this.img2d, sr, new RectF(left, top,
                left + width, top + height), null);
        if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
            return;
        }
        if (isselected && isLock == 0) {
            //缩放图标
            RectF rbRect = new RectF(left + width - scaleImageWidth/2,
                    top + height - scaleImageWidth/2,
                    left + width + scaleImageWidth/2,
                    top + height + scaleImageWidth/2);
            canvas.drawBitmap(puBitmap, new Rect(0, 0, puBitmap.getWidth(),
                    puBitmap.getHeight()), rbRect, null);

            //左上角删除图标
            float hl = this.left - this.deleteBitmap.getWidth() / 2;
            float ht = this.top - this.deleteBitmap.getHeight() / 2;
            float hr = this.left + this.deleteBitmap.getWidth() / 2;
            float hb = this.top + this.deleteBitmap.getHeight() / 2;
            Rect rs = new Rect(0, 0, this.deleteBitmap.getWidth(), this.deleteBitmap.getHeight());
            RectF ds = new RectF(hl, ht, hr, hb);
            canvas.drawBitmap(this.deleteBitmap, rs, ds, null);
        }
    }

    /**
     * 复制对象
     *
     * @param element 原始对象
     * @param barCodeOneElement 新的对象
     * @return 新的对象
     */
   public QrCodeElement clone(Element element, QrCodeElement barCodeOneElement) {
       barCodeOneElement.scale = element.scale; // 缩放比例
       barCodeOneElement.dataSourceRowIndex = element.dataSourceRowIndex; // 数据行号
       barCodeOneElement.ddStep = element.ddStep; // 递变数据 增长步长
       barCodeOneElement.dataSourceColName = element.dataSourceColName; // 数据源名称
       barCodeOneElement.dataSourceColIndex = element.dataSourceColIndex; // 数据源的列号
       barCodeOneElement.inputMode = element.inputMode; // 内容输入模式 0 手工 1 excel数据
       barCodeOneElement.isPrinter = element.isPrinter;  // 0 不是打印对象 1打印
       barCodeOneElement.rate = element.rate;  // 旋转方向
       barCodeOneElement.barcodeType = element.barcodeType; //默认qr code
       barCodeOneElement.encoding = element.encoding; //  编码方式 默认utf-8
       barCodeOneElement.left = element.left; //元素左侧距离
       barCodeOneElement.top = element.top; //元素顶部距离
       barCodeOneElement.errorCorrectionLevel = element.errorCorrectionLevel; //二维码纠错级别 默认中
       barCodeOneElement.datamode = element.datamode; // 数据模式 1 二进制模式，0 普通模式
       barCodeOneElement.isShowLogo = element.isShowLogo; //是否显示logo
       if (PutyConst.SERIES_TYPE == PutyConst.INDUSTRY_SERIES) {
           barCodeOneElement.isShow = element.isShow; // 是否显示
           barCodeOneElement.type = element.type; // 元素类型
           barCodeOneElement.isblack = element.isblack; // 黑白显示
           barCodeOneElement.isMirror = element.isMirror; // 镜像
       }
       if (PutyConst.SERIES_TYPE == PutyConst.HOUSEHOLD_SERIES) {
           barCodeOneElement.width = element.width; // 元素宽
           barCodeOneElement.height = element.height; // 元素高
       }
       return barCodeOneElement;
   }
}
